

import BallCol from "./ballCol";
import ColorType from "./enum/color";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BallTable extends cc.Component {


    // LIFE-CYCLE CALLBACKS:
    
    ball_nums: number[] = [];
    colorType: ColorType = ColorType.Red;
    Columns:  cc.Node[]
    // onLoad () {}

    start () {
        for (let index = 0; index < this.node.children.length; index++) {
            const element = this.node.children[index];
            element.removeAllChildren()
        }
    }

    touchColToOther(tempColums): { isCan: boolean, RescolIndex?: number, ball_nums?: number[] } {
        
        let lastBallScript
        let tempScript
        let lastBall
        try {
             lastBall = tempColums.children[0]
             lastBallScript = lastBall.getComponent("Ball")
             tempScript = tempColums.getComponent("ballCol")
        } catch (error) {
            return{ isCan: false }
        }

        let i = 0
        for (const col of this.node.children) {
            
            if (col.children.length != 0) {
                const firstBall = col.children[col.children.length - 1]
                const firstBallScript = firstBall.getComponent("Ball")
                const colScript = col.getComponent("ballCol")
                if (lastBallScript.nowColor == firstBallScript.nowColor && firstBallScript.nowNum - 1 == lastBallScript.nowNum) {
                    colScript.ball_nums = colScript.ball_nums.concat(tempScript.ball_nums)
                    colScript.DrawBalls(i)
                    tempScript.clear()
                    tempScript.ball_nums = []
                    let obj = {isCan: true, RescolIndex: i, ball_nums: colScript.ball_nums
                    }
                    
                    return obj
                }
            }
            i++;

        }
        return { isCan: false }
    }

    touchColToMe(tempColums): { isCan: boolean, colIndex?: number, ball_nums?: number[] } {
        let lastBall
        let lastBallScript
        let tempScript
        let i = 0

        try {
            lastBall = tempColums.children[tempColums._children.length - 1]
            lastBallScript  = lastBall.getComponent("Ball")
            tempScript = tempColums.getComponent("ballCol")
        } catch (error) {
            return{ isCan: false }
        }
        for (const col of this.node.children) {
            if (col.children.length != 0) {
                const firstBall = col.children[0]
                const firstBallScript = firstBall.getComponent("Ball")
                const colScript = col.getComponent("ballCol")
                if (lastBallScript.nowColor == firstBallScript.nowColor && firstBallScript.nowNum  == lastBallScript.nowNum -1) {
                    let balls = tempScript.ball_nums.concat(colScript.ball_nums)
                    return {
                        isCan: true,
                        colIndex:i,
                        ball_nums: balls
                    }
                }
            }
            i++
        }
        return { isCan: false }
    }
    // update (dt) {}
}
